Skip to content

Conversation

@nick-potts
Copy link

This is a continuation of #10, where it uses's Laravel's migrator instance to fetch custom migration paths.

Also added in the docs the ability to override getCustomMigrationPaths and getMigrationPaths.

Copy link

@Vignesh-Jothi Vignesh-Jothi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

General Suggestions

  1. Error Handling: Ensure all filesystem operations and external processes (like git commands) have adequate error handling.
  2. Logging: Consider logging significant actions and errors for easier debugging and monitoring.
  3. Testing: Ensure you have comprehensive tests covering these methods, especially focusing on edge cases and failure scenarios.

Overall, the code is well-organized and leverages Laravel's collection methods effectively. With the above considerations, it can be further improved for robustness and readability.

@gehrisandro
Copy link

Thanks @nick-potts This works perfectly fine.

Until this PR gets merged, you can extend the FastRefreshDatabase trait with a custom one like this:

<?php

namespace App\Support\Testing;

use SplFileInfo;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Process\Process;

trait FastRefreshDatabase
{
    use \Plannr\Laravel\FastRefreshDatabase\Traits\FastRefreshDatabase;

    protected function calculateMigrationChecksum(): string
    {
        // Filter out non-existing paths
        $paths = collect($this->getMigrationPaths())
            ->map(fn ($path) => realpath($path))
            ->toArray();

        $finder = Finder::create()
            ->in($paths)
            ->name('*.php')
            ->ignoreDotFiles(true)
            ->ignoreVCS(true)
            ->files();

        // Get all the migration files and their last modified date
        $migrations = collect(iterator_to_array($finder))
            ->map(fn (SplFileInfo $fileInfo) => [$fileInfo->getMTime()])
            // Reset the array keys so there is less data
            ->values()
            ->toArray();

        // Add the current git branch
        $checkBranch = new Process(['git', 'branch', '--show-current']);
        $checkBranch->run();

        $migrations['gitBranch'] = trim($checkBranch->getOutput());

        // Create a hash
        return hash('sha256', json_encode($migrations, JSON_THROW_ON_ERROR));
    }

    protected function getMigrationPaths(): array
    {
        return [
            database_path('migrations'),
            ...app('migrator')->paths(),
        ];
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants